package com.lych.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lych.dubbo.api.QuestionApi;
import com.lych.dubbo.api.SettingsApi;
import com.lych.model.domain.Question;
import com.lych.model.domain.Settings;
import com.lych.model.domain.UserInfo;
import com.lych.model.vo.PageResult;
import com.lych.model.vo.SettingsVo;
import com.lych.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    //查询通用设置
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //获取用户id
        Long userId = UserHolder.getUserId();
        vo.setId(userId);
        //获取用户手机号
        vo.setPhone(UserHolder.getMobile());
        //获取用户的陌生人问题
        Question question = questionApi.findById(userId);
        String txt = question == null ? "你爱超超嘛" : question.getTxt();
        vo.setStrangerQuestion(txt);
        //获取用户的APP通知开关数据
        Settings settings = settingsApi.findById(userId);
        if (settings != null){
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setLikeNotification(settings.getLikeNotification());
        }
        return vo;
    }

    /**
     * 设置陌生人问题 - 保存
     * @param content
     */
    public void saveQuestions(String content) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //根据id查询出问题
        Question question = questionApi.findById(userId);
        //判断是否存在
        if (question == null) {
            //不存在就保存
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            //存在就修改
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    /**
     * 通知设置 - 保存
     * @param map
     */
    public void saveSetting(Map map) {
        boolean likeNotification = (Boolean) map.get("likeNotification");
        boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (Boolean)  map.get("gonggaoNotification");
        //得到用户id
        Long userId = UserHolder.getUserId();
        //通过id查询通知
        Settings settings = settingsApi.findById(userId);
        //判断通知是否为空
        if (settings == null) {
            //保存
            settings = new Settings();
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setUserId(userId);
            settingsApi.save(settings);
        }else {
            //修改
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.update(settings);
        }
    }

    /**
     * 黑名单 - 翻页列表
     * @return
     */
    public PageResult blacklist(int page, int size) {
        //获得用户id
        Long userId = UserHolder.getUserId();
        //通过id查询该用户的黑名单信息
        Page<UserInfo> pageInfo = settingsApi.findByUserId(userId,page,size);
        //转化数据
        PageResult pr = new PageResult(page,size, (int) pageInfo.getTotal(),pageInfo.getRecords());
        //返回
        return pr;
    }

    /**
     * 黑名单 - 移除
     * @param blackUserId
     */
    public void blacklistDelete(Long blackUserId) {
        //得到用户id
        Long userId = UserHolder.getUserId();

        settingsApi.blacklistDelete(userId,blackUserId);
    }
}
